package csc.fresher.view;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.faces.flow.builder.ReturnBuilder;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import csc.fresher.commons.IDCardNumberValidator;
import csc.fresher.entity.CustomerAccountOverall;
import csc.fresher.entity.SystemUser;
import csc.fresher.service.DepositAccountService;
import csc.fresher.service.SystemUserService;

@Component
@Scope("session")
public class DepositManageBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private DepositAccountService depositManageService;

	@Autowired
	private SystemLoginManageBean systemLoginManageBean;

	@Autowired
	private SystemUserService systemUserService;

	private static Pattern pattern;
	private static Matcher matcher;
	private final String idCardNumber_pattern = "^\\s*\\d+\\s*$";
	private String selectSearchType;
	private List<CustomerAccountOverall> depositAccountList;
	private String searchValue;
	private CustomerOverallDataModel depositAccountListDataModel;
	private CustomerAccountOverall customerAccountOverall;

	public void onLoad() {
		SystemUser systemUser;

		if (systemLoginManageBean.getSystemUser() == null) {
			try {
				FacesContext.getCurrentInstance().getExternalContext()
						.redirect("/csc_final/templates3/Logon.jsf");
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			systemUser = systemLoginManageBean.getSystemUser();
			List<Long> listrole = systemUserService
					.getRoleByUserName(systemUser.getUserName());
			boolean result = checkRole(listrole);
			if (result == false) {
				try {
					FacesContext.getCurrentInstance().getExternalContext()
							.redirect("/csc_final/Group1/AccountManagement.xhtml");
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public boolean checkRole(List<Long> listRole) {

		for (int i = 0; i < listRole.size(); i++) {
			if (3 == listRole.get(i) || 4 == listRole.get(i)) {
				return true;
			}
		}
		return false;
	}

	@PostConstruct
	public void init() {
		this.customerAccountOverall = null;
		this.depositAccountList = firstLoad();
		this.depositAccountListDataModel = new CustomerOverallDataModel(depositAccountList);

	}

	// first load
	public List<CustomerAccountOverall> firstLoad() {
		depositAccountList = depositManageService.getAccountByState("active",
				"DepositAccount");
		return depositAccountList;
	}

	// search button
	public String search() {
		if(checkSearchValue(searchValue)){
		if ("idCardNumber".equals(selectSearchType)) {
			try{
		
			if(checkIdCardNumber(searchValue)){
				long idCardNumber =  Long.parseLong(searchValue);
				depositAccountList =depositManageService.getAccountByIDCardNumber(
						idCardNumber, "DepositAccount");
				
				depositAccountListDataModel = new CustomerOverallDataModel(depositAccountList);
			}
	
			}catch(Exception e){
				e.printStackTrace();
			}
			

		
			
		} else if ("fristName".equals(selectSearchType)) {
			depositAccountListDataModel = new CustomerOverallDataModel(
					depositManageService.getAccountByFirstName(searchValue,
							"DepositAccount"));
		} else if ("state".equals(selectSearchType)) {
			depositAccountListDataModel = new CustomerOverallDataModel(
					depositManageService.getAccountByState(searchValue,
							"DepositAccount"));
		} else if ("phoneNumber".equals(selectSearchType)) {
			depositAccountListDataModel = new CustomerOverallDataModel(
					depositManageService.getAccoungByPhoneNumber(searchValue,
							"DepositAccount"));
		} else if ("address".equals(selectSearchType)) {
			depositAccountListDataModel = new CustomerOverallDataModel(
					depositManageService.getAccountByAddress(searchValue,
							"DepositAccount"));
		}
		}
		return "DepositAccountManagement";
		
	}

	// edit button
	public String edit() {
		customerAccountOverall = null;
		customerAccountOverall = depositAccountListDataModel.getRowData();
		return "UpdateDepositAccount";
	}

	public String getSelectSearchType() {
		return selectSearchType;
	}

	public void setSelectSearchType(String selectSearchType) {
		this.selectSearchType = selectSearchType;
	}

	public List<CustomerAccountOverall> getDepositAccountList() {
		return depositAccountList;
	}

	public void setDepositAccountList(
			List<CustomerAccountOverall> depositAccountList) {
		this.depositAccountList = depositAccountList;
	}

	public String getSearchValue() {
		return searchValue;
	}

	public void setSearchValue(String searchValue) {
		this.searchValue = searchValue;
	}

	public DataModel<CustomerAccountOverall> getDepositAccountListDataModel() {
		return depositAccountListDataModel;
	}

	public void setDepositAccountListDataModel(
			CustomerOverallDataModel depositAccountListDataModel) {
		this.depositAccountListDataModel = depositAccountListDataModel;
	}

	public CustomerAccountOverall getCustomerAccountOverall() {
		return customerAccountOverall;
	}

	public void setCustomerAccountOverall(CustomerAccountOverall customerAccount) {
		this.customerAccountOverall = customerAccount;
	}

	// search and reload dataTable
	public String searchAccount() {
		depositAccountList = depositManageService.getAccountByIDCardNumber(
				Long.parseLong(searchValue), "DepositAccount");
		depositAccountListDataModel = new CustomerOverallDataModel(depositAccountList);
		return "";
	}
	
	public boolean checkSearchValue(String searchValue){
		boolean result = false;
		try {
		if(null == searchValue || searchValue == "" || "".equals(searchValue) || searchValue.isEmpty()){
			result = false;
		}else{
			result = true;
		}
		}catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}
	public boolean checkIdCardNumber(String idCardNumber) {

		pattern = Pattern.compile(idCardNumber_pattern);
		matcher = pattern.matcher(idCardNumber);
		return matcher.matches();

	}
}
